package com.sychip.fhc.app.data.source.dao


import androidx.room.Dao
import androidx.room.Query
import androidx.room.Upsert
import com.sychip.fhc.app.data.source.entity.WsnDeviceEntity
import kotlinx.coroutines.flow.Flow

const val TBL_WsnDevice = "wsnDevice"

/**
 * Wsn device dao
 *
 * @constructor Create empty Wsn device dao
 */
@Dao
interface WsnDeviceDao {

    /**
     * Observe all
     *
     * @param usbId
     * @return
     */
    @Query("SELECT * FROM $TBL_WsnDevice WHERE usbId = :usbId")
    fun observeAll(usbId: String): Flow<List<WsnDeviceEntity>>

    /**
     * Observe by id
     *
     * @param usbId
     * @param devId
     * @return
     */
    @Query("SELECT * FROM $TBL_WsnDevice WHERE usbId = :usbId and devId = :devId ")
    fun observeById(usbId: String, devId: String): Flow<WsnDeviceEntity>

    /**
     * Get all
     *
     * @param usbId
     * @return
     */
    @Query("SELECT * FROM $TBL_WsnDevice WHERE usbId = :usbId")
    suspend fun getAll(usbId: String): List<WsnDeviceEntity>

    /**
     * Get by id
     *
     * @param usbId
     * @param devId
     * @return
     */
    @Query("SELECT * FROM $TBL_WsnDevice WHERE usbId = :usbId and devId = :devId")
    suspend fun getById(usbId: String, devId: String): WsnDeviceEntity?

    /**
     * Upsert
     *
     * @param bleDevice
     */
    @Upsert
    suspend fun upsert(bleDevice: WsnDeviceEntity)

    /**
     * Upsert all
     *
     * @param tasks
     */
    @Upsert
    suspend fun upsertAll(tasks: List<WsnDeviceEntity>)

    /**
     * Delete by id
     *
     * @param usbId
     * @param devId
     * @return
     */
    @Query("DELETE FROM $TBL_WsnDevice WHERE usbId = :usbId and devId = :devId")
    suspend fun deleteById(usbId: String, devId: String): Int

    /**
     * Delete all
     *
     */
    @Query("DELETE FROM $TBL_WsnDevice WHERE usbId = :usbId")
    suspend fun deleteAll(usbId: String)

}
